package top.yehot.cmmu.shiro;

import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

import top.yehot.cmmu.shiro.session.CustomSessionFactory;
import top.yehot.cmmu.shiro.session.RedisSessionDAO;

/**
 * shiro 自动化配置
 * 
 * @author lquan
 * @date 2022-9-10 16:55:36
 */
public class ShiroAutoConfig {
	
	@Value("#{ @environment['shiro.sessionManager.globalSessionTimeout'] ?: 30 }")
    protected Integer globalSessionTimeout;
	
	/**
	 * 配置session工厂
	 */
	@Bean
	public SessionFactory sessionFactory() {
		return new CustomSessionFactory();
	}

	/**
	 * 配置session操作
	 */
	@Bean
	@ConditionalOnProperty(prefix = "shiro.cache", name = "type", havingValue = "redis")
	public SessionDAO sessionDAO(RedissonClient redissonClient) {
		RedisSessionDAO rsd = new RedisSessionDAO();
		rsd.setGlobalSessionTimeout(globalSessionTimeout);
		rsd.setRedissonClient(redissonClient);
        return rsd;
    }

}
